from google.appengine.ext import db

class Activity(db.Model):
    name = db.StringProperty()
    type = db.StringProperty()
    
    def save(self):
        self.put()
        self.id = self.key().id()

def LoadOrCreateActivity(name, type=None):
    activityQ = db.GqlQuery("SELECT * FROM Activity WHERE name=:1", name)
    activity = activityQ.get()
    if not activity:
        activity = Activity()
        activity.name = name
        activity.type = type
        activity.put()
    return activity

def LoadAllActivities(maxEntries=200):
    actEntryQ = db.GqlQuery("SELECT * FROM Activity")
    entries = actEntryQ.fetch(maxEntries)
    for e in entries:
        e.id = e.key().id()
    return entries

def LoadActivityFromId(id):
    key = db.Key.from_path('Activity', long(id))
    a = db.get(key)
    if not a:
        return None
    a.id = a.key().id()
    return a

